var ani = ani || {};
ani.ui = ani.ui || {};
ani.ui.template = ani.ui.template || {};

//detail page:
/**
 * detail page constructor
 * @param detailPage
 * @param callParamsObj
 */
ani.ui.template.detail = function(detailPage, callParamsObj){
    var detailContainer = ani.ui.template.object.constructDOMContainer(detailPage);
    var detailObjectsMap = ani.ui.callServer(
        detailPage.targetUrl,
        callParamsObj,   //{appIds: ***}
        'GET'
    );

    for(var onePageFieldObjIdx in detailPage.objects){
        detailContainer.append(
            ani.ui.template.object.constructDom(
                detailPage.objects[onePageFieldObjIdx],
                detailObjectsMap
            )
        );
    }
    return detailContainer;
}
/**
 * list page constructor
 * @param listPage
 * @param callParamsObj
 */
ani.ui.template.list = function(listPage, callParamsObj){

    var generalListContainer = ani.ui.template.object.constructDOMContainer(listPage);

    var constructOnePageList = function(oneListObject, pageNumberObj){
        if(!callParamsObj){
            callParamsObj = {};
        }
        //get page number
        var pageParamName = oneListObject.pageNumParamKey;
        if(!pageNumberObj.curPageNumber){
            pageNumberObj.curPageNumber = 0;
        }
        callParamsObj[pageParamName] = pageNumberObj.curPageNumber;

        //start request
        var listData = ani.ui.callServer(
            listPage.targetUrl,
            callParamsObj,   //{classificationId: ***}
            'GET'
        );
        var constructPageContainer = function(){
            var pageDivideLabel = ani.ui.generateHTMLMark('section');
            pageDivideLabel.attr('class', 'pageContainer');
            return pageDivideLabel;
        }
        var constructPageNumber = function(){
            if(!oneListObject.totalPageNumFieldName){
                return null;
            }
            pageNumberObj.totalPageNumber = listData[oneListObject.totalPageNumFieldName];
            var pageDivideLabel = ani.ui.generateHTMLMark('section');
            pageDivideLabel.attr('class', 'pageDivideLabel');
            var pageExp = new ani.util.Expression(
                oneListObject.pageDivideLabel,  //example: 'PAGE_{page}'
                {
                    page: ++pageNumberObj.curPageNumber,
                    totalPageNum: pageNumberObj.totalPageNumber
                }
            ).getFullResult();
            pageDivideLabel.html(pageExp);
            return pageDivideLabel;
        }
        //start construct
        var onePageContainer = constructPageContainer();
        onePageContainer.append(constructPageNumber());
        var listObjects = ani.ui.generateHTMLMark('ul');
        for(var oneDataObjectIdx in listData.objects){   //list data objects
            var listObjectContainer = ani.ui.generateHTMLMark('li');
            for(var oneListObjIdx in oneListObject.objects){ //data format
                listObjectContainer.append(
                    ani.ui.template.object.constructDom(
                        oneListObject.objects[oneListObjIdx],
                        listData.objects[oneDataObjectIdx],
                        false
                    )
                );
            }
            listObjects.append(listObjectContainer);
        }
        onePageContainer.append(listObjects);
        return onePageContainer;
    }
    //next page button class
    var NextPageButton = function(pageObj, pageNumberObj, onePageContainer){
        var totalPage = pageNumberObj.totalPageNumber;
        var curPageNumber = pageNumberObj.curPageNumber;
        var _this = this;
        var nextPageButton = ani.ui.generateHTMLMark('button');
        nextPageButton.attr('type', 'button');
        nextPageButton.attr('class', 'nextPageButton');
        nextPageButton.html('more');

        var onClick = function(){
            this.process = function(){
                onePageContainer.append(constructOnePageList(pageObj, curPageNumber+1));
                renewButton();
            }
        }
        var renewButton = function(){
            if(totalPage > curPageNumber + 1){
                nextPageButton.bind(
                    'click',
                    function(){
                        new onClick().process();
                    }
                );
            }else{
                nextPageButton.unbind('click');
                nextPageButton.css('display', 'none');
            }
        }
        renewButton();
        return nextPageButton;
    }
    var generateListPagesContainer = function(){
        var listPageContainer = ani.ui.generateHTMLMark('section');
        listPageContainer.attr('class', listPageContainer);
        return listPageContainer;
    }
    //generate list general list containers & push into outer list section
    for(var oneListIdx in listPage.objects){
        var pageNumberObj = {
            curPageNumber: 0,
            totalPageNumber: 0
        }
        var listPageContainer = generateListPagesContainer();
        var onePageContainer = constructOnePageList(listPage.objects[oneListIdx], pageNumberObj);
        listPageContainer.append(
            onePageContainer
        );
        generalListContainer.append(listPageContainer);
        generalListContainer.append(new NextPageButton(listPage.objects[oneListIdx], pageNumberObj, onePageContainer));
    }

    return generalListContainer;
}